<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Feb 25, 2012
 * Time: 6:14:23 PM
 * To change this template use File | Settings | File Templates.
 */
     class BaseController{
        //public $path;
        public $theme;
        public $controllerID;
        public $userPermission;
        protected $session;
        protected $message;

        public function render($name, $data=null){
         $header = new BaseView('themes/'.$this->theme,'header_template');
         $footer = new BaseView('themes/'.$this->theme,'footer_template');
         $master = new BaseView('themes/'.$this->theme,'master_template');
         $content = new BaseView('views/'.$this->controllerID,$name);
          if($data!=null && is_array($data)){
              foreach($data as $key=>$value){
                 $content->$key = $value;
              }
          }
         $master->assign('header', $header->render(FALSE));
         $master->assign('footer', $footer->render(FALSE));
         $master->assign('content', $content->render(FALSE));
         $master->assign('message',$this->message);
         $master->render();
        }

        public function renderPartial($name, $data=null){
          $content = new BaseView('views/'.$this->controllerID,$name);
          if($data!=null && is_array($data)){
              foreach($data as $key=>$value){
                $content->$key = $value;
              }
          }
          $content->render();
        }
         public function action(){

         }
         public function actionError(){
             if(isset($_SESSION['user_id']))  {
                    $this->urlRedirect('site/noPage');
             }else{
                  $this->urlRedirect('site/login');
             }
         }
         /* This function determines and returns an absolute URL.
         * It takes one argument: the page that concludes the URL.
         * The argument defaults to index.php.
         */
        public function absolute_url ($page = 'site') {
            $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);            
            $url = rtrim($url, '/\\');   // Remove any trailing slashes:
            $url .= '/index.php?' . $page;             
            return $url;

        } // End of absolute_url() function.
         public function urlRedirect($arg){
            $url = $this->absolute_url($arg);
            header("Location: $url");
            exit();
        }
         public function __construct($controllerID){
               $this->controllerID = $controllerID;
               $this->theme = CmbBaseConfig::getConfig('DefaultTheme');
               $this->session = new SessionManager();
               $this->checkSession();
         }
         public function checkSession(){
             if($this->controllerID!='site'){
                 $userId = $this->session->getSession('user_id');
                  if(!isset($userId)){
                      $this->urlRedirect('site/login');
                  }
                 $this->userPermission = $_SESSION['permission'];
             }              
         }

    }
 
